package com.kaily.imagelib.element;

import android.graphics.Bitmap;
import android.graphics.Canvas;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.Target;
import com.kaily.imagelib.BaseApplication;
import com.kaily.imagelib.IMergeImageElement;
import com.kaily.imagelib.utils.BitmapUtils;

/**
 * Image元素
 *
 * @author kaily_zhou
 * @date 2018/10/19 下午1:59
 */
public class ImageElement implements IMergeImageElement {
    public static final int IMG_LOAD_TYPE_ORIGINAL = 1;
    public static final int IMG_LOAD_TYPE_GLIDE = 2;
    protected String imgUrl;
    protected boolean isRequireElement;
    protected Bitmap bitmap;
    protected int width;
    protected int height;
    private float left;
    private float top;
    private Bitmap orignBitmap;
    private int imgLoadType = IMG_LOAD_TYPE_ORIGINAL;
    //是否以宽度为标准进行缩放
    private boolean isWidthStandard;

    public ImageElement(String imgUrl, boolean isRequireElement, float left, float top) {
        this.imgUrl = imgUrl;
        this.isRequireElement = isRequireElement;
        this.left = left;
        this.top = top;
    }

    public ImageElement(Bitmap bitmap, boolean isRequireElement, float left, float top) {
        this.orignBitmap = bitmap;
        this.isRequireElement = isRequireElement;
        this.left = left;
        this.top = top;
    }

    public ImageElement(String imgUrl, boolean isRequireElement, float left, float top, int width, int height) {
        this(imgUrl, isRequireElement, left, top);
        this.width = width;
        this.height = height;
    }

    public ImageElement(String imgUrl, boolean isRequireElement, float left, float top, int width, int height, int imgLoadType) {
        this(imgUrl, isRequireElement, left, top);
        this.width = width;
        this.height = height;
        this.imgLoadType = imgLoadType;
    }

    public ImageElement(String imgUrl, boolean isRequireElement, float left, float top, int width, int height, int imgLoadType, boolean isWidthStandard) {
        this(imgUrl, isRequireElement, left, top, width, height, imgLoadType);
        this.isWidthStandard = isWidthStandard;
    }

    @Override
    public boolean generateElement() {
        if (orignBitmap != null) {
            bitmap = orignBitmap;
            return true;
        }
        if (imgLoadType == IMG_LOAD_TYPE_GLIDE) {
            //使用glide加载
            try {
                bitmap = Glide.with(BaseApplication.getAppContext())
                        .load(imgUrl)
                        .asBitmap()
                        .centerCrop()
                        .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).get();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            //使用原始加载
            bitmap = BitmapUtils.getBitmapFormUrl(imgUrl);
        }
        if (!isWidthStandard) {
            if (bitmap != null && width > 0 && height > 0) {
                bitmap = BitmapUtils.zoomBitmap(bitmap, width, height);
            }
        } else {
            if (bitmap != null && width > 0) {
                int originalWidth = bitmap.getWidth();
                if (originalWidth > 0) {
                    float ratio = (float) width / (float) originalWidth;
                    bitmap = BitmapUtils.scaleBitmap(bitmap, ratio);
                    if (bitmap != null) {
                        height = bitmap.getHeight();
                    }
                } else {
                    bitmap = null;
                }
            } else {
                bitmap = null;
            }
        }

        if (isRequireElement) {
            return bitmap != null;
        } else {
            return true;
        }
    }

    public int getHeight() {
        return height;
    }

    @Override
    public boolean mergeImage(Canvas canvas) {
        if (canvas == null) {
            return false;
        }
        if (bitmap == null) {
            return !isRequireElement;
        }
        try {
            canvas.drawBitmap(bitmap, left, top, null);
            return true;
        } catch (Exception e) {
            return !isRequireElement;
        }
    }

    @Override
    public boolean isRequireElement() {
        return isRequireElement;
    }
}
